package com.ruoyi.xiaoshou.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.xiaoshou.domain.SaleOrder;
import com.ruoyi.xiaoshou.dto.UpdateOrderDto;
import com.ruoyi.xiaoshou.service.ISaleOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 销售订单Controller
 *
 * @author ruoyi
 * @date 2024-10-30
 */
@RestController
@RequestMapping("/order")
public class SaleOrderController extends BaseController {
    @Autowired
    private ISaleOrderService saleOrderService;

    private Logger logger = LoggerFactory.getLogger(SaleOrderController.class);

    /**
     * 查询销售订单列表
     */
    @RequiresPermissions("xiaoshou:order:list")
    @GetMapping("/list")
    public TableDataInfo list(SaleOrder saleOrder) {
        startPage();
        List<SaleOrder> list = saleOrderService.selectSaleOrderList(saleOrder);
        logger.info("list:{}", list);
        return getDataTable(list);
    }

    /**
     * @return com.ruoyi.common.core.web.page.TableDataInfo
     * @description 用于销售收款单的查询销售订单
     * @date 2024/11/12 上午10:16
     * @Param saleOrder:
     **/
//    @RequiresPermissions("xiaoshou:order:list")
    @GetMapping("/listForReciveOrder")
    public TableDataInfo listForReciveOrder(SaleOrder saleOrder) {
        startPage();
        List<SaleOrder> list = saleOrderService.selectSaleOrderListForReciveOrder(saleOrder);
        logger.info("listForReciveOrder list:{}", list);
        return getDataTable(list);
    }


    @RequiresPermissions("xiaoshou:order:list")
    @GetMapping("/noReconList")
    public TableDataInfo noReconList(SaleOrder saleOrder)
    {
        startPage();
        List<SaleOrder> list = saleOrderService.noReconList(saleOrder);
        logger.info("noReconList:{}", list);
        return getDataTable(list);
    }

    @PostMapping("getOrderByCode")
    public SaleOrder getOrderByCode(@RequestBody String orderCode) {
        logger.info("SaleOrderController getOrderByCode params:{}", orderCode);
        SaleOrder saleOrder = saleOrderService.getOrderByCode(orderCode);
        return saleOrder;
    }

    /**
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     * @description 根据编号数组获取销售订单集合
     * @date 2024/11/12 上午9:16
     * @Param orderCodes:
     **/
    @GetMapping("/getSaleOrderListByOrderCodes/{codes}")
    public AjaxResult getSaleOrderListByOrderCodes(@PathVariable("codes") String[] codes) {
        logger.info("SaleOrderController getSaleOrderListByOrderCodes params:{}", Arrays.toString(codes));
        List<SaleOrder> saleOrderList = saleOrderService.getSaleOrderListByOrderCodes(codes);
        return AjaxResult.success(saleOrderList);
    }

    /**
     * 导出销售订单列表
     */
    @RequiresPermissions("xiaoshou:order:export")
    @Log(title = "销售订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SaleOrder saleOrder) {
        List<SaleOrder> list = saleOrderService.selectSaleOrderList(saleOrder);
        ExcelUtil<SaleOrder> util = new ExcelUtil<SaleOrder>(SaleOrder.class);
        util.exportExcel(response, list, "销售订单数据");
    }

    /**
     * 获取销售订单详细信息
     */
    @RequiresPermissions("xiaoshou:order:query")
    @GetMapping(value = "/{Id}")
    public AjaxResult getInfo(@PathVariable("Id") Integer Id) {
        return success(saleOrderService.selectSaleOrderById(Id));
    }
    /**
     * 获取销售订单今日笔数及金额
     */
    @RequiresPermissions("xiaoshou:order:query")
    @GetMapping(value = "getSaleOrderCount")
    public AjaxResult getSaleOrderCount() {
        return success(saleOrderService.getSaleOrderCount());
    }

    /**
     * 获取销售订单各阶段的数量
     * @return
     */
    @RequiresPermissions("xiaoshou:order:query")
    @GetMapping(value = "getThreeCountToIndex")
    public AjaxResult getThreeCountToIndex() {
        return success(saleOrderService.getThreeCountToIndex());
    }
    @RequiresPermissions("xiaoshou:order:getList")
    @RequestMapping("getList")
    public AjaxResult getList() {
        return success(saleOrderService.getAllList());
    }

    /**
     * 新增销售订单
     */
    @RequiresPermissions("xiaoshou:order:add")
    @GetMapping(value = "addSaleOrder/{Id}")
    public AjaxResult addSaleOrder(@PathVariable("Id") Integer Id) {
        return success(saleOrderService.insertSaleOrder(Id));
    }

    /**
     * 修改销售订单
     */
    @InnerAuth
    @RequiresPermissions("xiaoshou:order:edit")
    @Log(title = "销售订单", businessType = BusinessType.UPDATE)
    @PostMapping("updSaleOrder")
    public int edit(@RequestBody SaleOrder saleOrder) {
        return saleOrderService.updateSaleOrder(saleOrder);
    }


    @RequiresPermissions("xiaoshou:order:edit")
    @PostMapping("updateSaleOrderStatus")
    public AjaxResult updateSaleOrderStatus(@RequestBody UpdateOrderDto updateOrderDto) {
        return success(saleOrderService.updateSaleOrderStatus(updateOrderDto));
    }

    /**
     * 删除销售订单
     */
    @RequiresPermissions("xiaoshou:order:remove")
    @Log(title = "销售订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{Ids}")
    public AjaxResult remove(@PathVariable Integer[] Ids) {
        return toAjax(saleOrderService.deleteSaleOrderByIds(Ids));
    }
}
